package com.waterworld.watch.mine.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.bumptech.glide.Glide;
import com.waterworld.watch.R;
import com.waterworld.watch.common.bean.UserInfoBean;
import com.waterworld.watch.common.customview.CircleImageView;
import com.waterworld.watch.common.customview.CustomDialog;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.net.UrlContants;
import com.waterworld.watch.common.util.AppManager;
import com.waterworld.watch.common.util.PhotoUtils;
import com.waterworld.watch.common.util.ToastUtils;
import com.waterworld.watch.home.activity.HomePagerActivity;
import com.waterworld.watch.home.event.UserInfoEvent;
import com.waterworld.watch.login.activity.LoginActivity;
import com.waterworld.watch.login.interfaces.ILoginManager;
import com.waterworld.watch.login.manager.LoginManager;
import com.waterworld.watch.mine.activity.*;
import com.waterworld.watch.mine.interfaces.IMineManager;
import com.waterworld.watch.mine.manager.MineManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MineFragment extends Fragment implements View.OnClickListener {

    @BindView(R.id.cl_myInfo)
    ConstraintLayout cl_myInfo;

    @BindView(R.id.parent_avatar)
    CircleImageView parent_avatar;

    @BindView(R.id.tv_parent_name)
    TextView tv_parent_name;

    @BindView(R.id.tv_watchPhone)
    TextView tv_watchPhone;

    @BindView(R.id.cl_notification)
    ConstraintLayout notification;

    @BindView(R.id.cl_intimate)
    ConstraintLayout intimate;

    @BindView(R.id.cl_timing)
    ConstraintLayout timing;

    @BindView(R.id.cl_shutdown)
    ConstraintLayout shutdown;

    @BindView(R.id.cl_clean)
    ConstraintLayout clean;

    @BindView(R.id.cl_update)
    ConstraintLayout update;

    @BindView(R.id.btn_outLogin)
    Button outLogin;

    private HomePagerActivity parent;

    private CustomDialog mDialog;
    private ILoginManager iLoginManager = LoginManager.getInstance();
    private IMineManager iMineManager = MineManager.getInstance();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        parent = (HomePagerActivity) getActivity();
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_mine,container,false);
        ButterKnife.bind(this,view);

        cl_myInfo.setOnClickListener(this);
        notification.setOnClickListener(this);
        intimate.setOnClickListener(this);
        timing.setOnClickListener(this);
        shutdown.setOnClickListener(this);
        clean.setOnClickListener(this);
        update.setOnClickListener(this);
        outLogin.setOnClickListener(this);
        EventBus.getDefault().register(this);
        return view;
    }

    private void initData() {
        if(UserInfoBean.getInstance().getUser().getHead() == null) {
            Glide.with(this).load(R.drawable.headimage_default).into(parent_avatar);
        } else {
            Glide.with(this).load(UserInfoBean.getInstance().getUser().getHead().startsWith("sys") ?
                    PhotoUtils.getResource(UserInfoBean.getInstance().getUser().getHead().substring(0,13)) :
                    UrlContants.FILE_URL + "resources/watch/" + UserInfoBean.getInstance().getUser().getHead()).into(parent_avatar);
        }
        if (UserInfoBean.getInstance().getUser().getName() == null) {
            tv_parent_name.setText(getString(R.string.common_default));
        } else {
            tv_parent_name.setText(UserInfoBean.getInstance().getUser().getName());
        }
        if (UserInfoBean.getInstance().getWatchUser().getPhone() == null) {
            tv_watchPhone.setText(getString(R.string.common_default));
        } else {
            tv_watchPhone.setText(UserInfoBean.getInstance().getWatchUser().getPhone());
        }

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cl_myInfo:
                startActivity(new Intent(parent,MyInformationActivity.class));
                break;
            case R.id.cl_notification:
                startActivity(new Intent(parent, NotificationCategoryActivity.class));
                break;
            case R.id.cl_intimate:
                startActivity(new Intent(parent,IntimateActivity.class));
                break;
            case R.id.cl_timing:
                startActivity(new Intent(parent,TimingActivity.class));
                break;
            case R.id.cl_shutdown:
                mDialog = new CustomDialog(parent, getString(R.string.warm_prompt), getString(R.string.confirm_close_watch),new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                        //关闭手表逻辑
                        if (iMineManager != null) {
                            iMineManager.remoteShutdown(new BaseObserverListener() {
                                @Override
                                public void onCompleted() {
                                    ToastUtils.showShort(parent,getString(R.string.success_close_watch));
                                }

                                @Override
                                public void onError(Throwable e) {
                                }

                                @Override
                                public void onNext(Object o) {
                                }
                            });
                        }
                    }
                }, new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                    }
                },getString(R.string.confirm),getString(R.string.cancel));
                mDialog.setCanotBackPress();
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.show();
                break;
            case R.id.cl_clean:
                mDialog = new CustomDialog(parent, getString(R.string.warm_prompt), getString(R.string.confirm_clear_cache),new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                        //清除缓存逻辑
                    }
                }, new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                    }
                },getString(R.string.confirm),getString(R.string.cancel));
                mDialog.setCanotBackPress();
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.show();
                break;
            case R.id.cl_update:
                mDialog = new CustomDialog(parent, getString(R.string.warm_prompt), getString(R.string.confirm_update_version),new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();
                    //版本更新逻辑
                }
            }, new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();
                }
            },getString(R.string.confirm),getString(R.string.cancel));
                mDialog.setCanotBackPress();
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.show();
                break;
            case R.id.btn_outLogin:
                mDialog = new CustomDialog(parent, getString(R.string.warm_prompt), getString(R.string.confirm_logout),new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
//                        if (iLoginManager != null) {
//                            iLoginManager.logout(new BaseObserverListener() {
//                                @Override
//                                public void onCompleted() {
//
//                                }
//
//                                @Override
//                                public void onError(Throwable e) {
//                                    ToastUtils.showShort(parent, getString(R.string.data_error));
//                                }
//
//                                @Override
//                                public void onNext(Object o) {
                                    CloudPushService pushService = PushServiceFactory.getCloudPushService();
                                    pushService.unbindAccount(new CommonCallback() {
                                        @Override
                                        public void onSuccess(String s) {

                                        }

                                        @Override
                                        public void onFailed(String s, String s1) {

                                        }
                                    });
                        startActivity(new Intent(parent, LoginActivity.class));
                        AppManager.finishActivity(HomePagerActivity.class);
                                    //清除数据库登录信息
//                                }
//                            });
//                        }
                    }
                }, new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                    }
                },getString(R.string.confirm),getString(R.string.cancel));
                mDialog.setCanotBackPress();
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.show();
                break;
            default:
                break;
        }
    }

    @Subscribe(sticky = true ,threadMode = ThreadMode.MAIN)
    public void loadData(UserInfoEvent userInfoEvent){
        if(userInfoEvent.isLoad()) {
            initData();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }
}
